package com.kwin.excel.util;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.util.DateUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author : wangkai
 * @version V1.0
 * @Package com.kwin.excel.util
 * @Description:
 * @date 2021年10月19日 10:44
 **/
public enum LocalDateTimeStringConverter implements Converter<LocalDateTime> {
    INSTANCE;
    
    private static final String MINUS = "-";
    
    @Override
    public Class supportJavaTypeKey() {
        return LocalDateTime.class;
    }
    
    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }
    
    @Override
    public LocalDateTime convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty,
        GlobalConfiguration globalConfiguration) throws Exception {
        String stringValue = cellData.getStringValue();
        String pattern;
        if (excelContentProperty == null || excelContentProperty.getDateTimeFormatProperty() == null) {
            pattern = switchDateFormat(stringValue);
        }
        else {
            pattern = excelContentProperty.getDateTimeFormatProperty().getFormat();
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return LocalDateTime.parse(cellData.getStringValue(), formatter);
    }
    
    @Override
    public CellData convertToExcelData(LocalDateTime localDateTime, ExcelContentProperty excelContentProperty,
        GlobalConfiguration globalConfiguration) throws Exception {
        String pattern;
        if (excelContentProperty == null || excelContentProperty.getDateTimeFormatProperty() == null) {
            pattern = DateUtils.DATE_FORMAT_19;
        }
        else {
            pattern = excelContentProperty.getDateTimeFormatProperty().getFormat();
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return new CellData<>(localDateTime.format(formatter));
    }
    
    /**
     * switch date format
     * @param dateString dateString
     * @return pattern
     */
    private static String switchDateFormat(String dateString) {
        int length = dateString.length();
        switch (length) {
            case 19:
                if (dateString.contains(MINUS)) {
                    return DateUtils.DATE_FORMAT_19;
                }
                else {
                    return DateUtils.DATE_FORMAT_19_FORWARD_SLASH;
                }
            case 17:
                return DateUtils.DATE_FORMAT_17;
            case 14:
                return DateUtils.DATE_FORMAT_14;
            case 10:
                return DateUtils.DATE_FORMAT_10;
            default:
                throw new IllegalArgumentException("can not find date format for：" + dateString);
        }
    }
}
